package com.handpay.framework.utils;

import android.util.Log;

import com.handpay.zztong.hp.base.fragment.BaseFragment;

public class ZZTongThread extends Thread {
	BaseFragment fragment;
	int viewLength = 0;
	int vPosition = 0;
	private boolean isStop = true;

	public ZZTongThread() {// 构造器
	}
	
	public void setViewLength(int viewLength) {
		this.viewLength = viewLength;
	}

	public void setPageViews(BaseFragment fragment) {
		this.fragment = fragment;
		if (this.getState() == State.RUNNABLE) {
			Log.e("TAG", "TAG State.RUNNABLE");
		} else if (this.getState() == State.NEW) {
			Log.e("TAG", "TAG State.NEW");
		} else if (this.getState() == State.BLOCKED) {
			Log.e("TAG", "TAG State.BLOCKED");
		} else if (this.getState() == State.TERMINATED) {
			Log.e("TAG", "TAG State.TERMINATED");
		} else if (this.getState() == State.TIMED_WAITING) {
			Log.e("TAG", "TAG State.TIMED_WAITING");
		} else if (this.getState() == State.WAITING) {
			Log.e("TAG", "TAG State.WAITING");
		}
		if (isAlive()) {
			Log.e("TAG", "TAG isAlive");
		}
		Log.e("TAG", "setPageViews");
			start();
	}
	
	private boolean isSleep = true;

	@Override
	public void run() {// 重写的run方法
		while (isStop) {// 循环
//			HPLog.e("--------------ZZTongThread------------------", "isSleep:" + isSleep);
			if (isSleep) {

				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else {
				if (vPosition < viewLength) {
					Log.w("TAG", "TAG run" + vPosition);
					if(fragment != null){
						fragment.handler.sendEmptyMessage(vPosition);
					}
					vPosition++;
					try {
						Thread.sleep(5000);// 睡眠
					} catch (Exception e) {
						e.printStackTrace();// 打印异常信息
					}
				} else {
					vPosition = 0;
				}
			}
		}
	}

	public void setViewPostion(int position) {
		Log.w("TAG", "TAG setViewPostion" + position);
		vPosition = position;
	}

	 public void setSleep(boolean sleep) {
		 if(viewLength > 1){
			 this.isSleep = sleep;   
		 }
     }    
     public void setStop(boolean stop) {
    	 if(viewLength > 1){
	         this.isStop = stop;   
	         interrupt();
    	 }
     }
}
